"""
#https://www.kuaidaili.com/free/
http://www.66ip.cn/
http://www.data5u.com/free/index.html

"""
import requests
import time
import random
import re
import telnetlib
from fake_useragent import UserAgent

ua = UserAgent()

header={
    'User-Agent': ua.random
}

def getip(u):
    start_url = "https://www.xicidaili.com/wn/{}".format(u)
    # proxies = {
    #       'https': 'https://122.246.53.18:8010',
    #     }
    print(ua.random)
    start_content = requests.get(start_url,headers=header).text
    # <td data-title="IP">101.132.174.110</td><td data-title="PORT">8088</td>
    # print(start_content) <td data-title="IP">(.*?)</td>.*?<td data-title="PORT">(.*?)</td>
    reg = re.compile(r'alt="Cn" /></td>.*?<td>(.*?)</td>.*?<td>(.*?)</td>.*?<td>', re.S)

    regs = re.findall(reg,start_content)
    print('正在获取第 %d 页代理中（请耐心等候）......' % u)
    for i in regs:
        # print(i[0],i[1])
        ips = str(i[0])+":"+str(i[1])
        print(ips)
        verifyProxyIP(i[0],i[1])
        time.sleep(1)
    print('第 %d 页代理获取完毕！' % u)
    print('------------------------------------')
    time.sleep(2)
# 验证获取到的代理IP是否可用
def verifyProxyIP(verify_ip, verify_ip_port):
    print('正在验证此代理IP是否可用......')
    try:
        telnetlib.Telnet(verify_ip, verify_ip_port, timeout=5)
    except:
        print('此代理IP不可用')
        print('-------------------------')
    else:
        print('此代理IP可用')
        print('-------------------------')
        available_ip = verify_ip + ':' + verify_ip_port
        with open("ip2.txt","a+") as ipss:
            ipss.write(available_ip+"\n")

for u in range(1,2000):
    print(u)
    getip(u)
# getip()